(function () {
    var docElem = window.document.documentElement, didScroll, scrollPosition;

    function noScrollFn() {
        window.scrollTo(scrollPosition ? scrollPosition.x : 0, scrollPosition ? scrollPosition.y : 0);
    }

    function noScroll() {
        window.removeEventListener('scroll', scrollHandler);
        window.addEventListener('scroll', noScrollFn);
    }

    function scrollFn() {
        window.addEventListener('scroll', scrollHandler);
    }

    function canScroll() {
        window.removeEventListener('scroll', noScrollFn);
        scrollFn();
    }

    function scrollHandler() {
        if (!didScroll) {
            didScroll = true;
            setTimeout(function () {
                scrollPage();
            }, 60);
        }
    };

    function scrollPage() {
        scrollPosition = {x: window.pageXOffset || docElem.scrollLeft, y: window.pageYOffset || docElem.scrollTop};
        didScroll = false;
    };

    scrollFn();

    [].slice.call(document.querySelectorAll('.morph-button')).forEach(function (bttn) {
        new UIMorphingButton(bttn, {
            closeEl: '.icon-close',
            onBeforeOpen: function () {
                // don't allow to scroll
                noScroll();
            },
            onAfterOpen: function () {
                // can scroll again
                canScroll();
            },
            onBeforeClose: function () {
                // don't allow to scroll
                noScroll();
            },
            onAfterClose: function () {
                // can scroll again
                canScroll();
            }
        });
    });

    // for demo purposes only
    [].slice.call(document.querySelectorAll('form #btn')).forEach(function (bttn) {
        bttn.addEventListener('click', function (ev) {
            ev.preventDefault();
        });
    });
})();